package rapier.sso.server.aspect;

import java.lang.reflect.Method;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import rapier.sso.common.utils.IPUtils;
import rapier.sso.common.utils.SpringContextUtils;
import rapier.sso.common.utils.ThreadLocalUtil;
import rapier.sso.core.entity.TDbSysAppLogEntity;
import rapier.sso.core.service.ITDbSysAppLogService;
import rapier.sso.core.service.ITDbSysUserLogService;
import rapier.sso.server.aspect.annotation.AutoLog;

@Aspect
@Component
@Order(100)
public class RapierSsoAspect {

	@Autowired
	private ITDbSysAppLogService appLogService;
	@Autowired
	private ITDbSysUserLogService userLogService;

	@Pointcut("@annotation(com.rapier.sso.server.aspect.annotation.AutoLog)")
	public void autoLog() {
	}


	@Around("autoLog()")
	public Object doLogAround(ProceedingJoinPoint point) throws Throwable {
		// 执行请求
		long beginTime = System.currentTimeMillis();
		// 执行方法
		Object result = point.proceed();
		// 执行时长(毫秒)
		long time = System.currentTimeMillis() - beginTime;
		saveAppLog(point, time);
		// 保存日志
		ThreadLocalUtil.clear();
		return result;
	}

	private void saveAppLog(ProceedingJoinPoint joinPoint, long time) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();

		TDbSysAppLogEntity appLog = new TDbSysAppLogEntity();
		AutoLog syslog = method.getAnnotation(AutoLog.class);
		if (syslog != null) {
			// 注解上的描述,操作日志内容
			appLog.setLogContent(syslog.value());
			appLog.setLogType(syslog.logType());
		}

		// 请求的方法名
		String className = joinPoint.getTarget().getClass().getName();
		String methodName = signature.getName();
		appLog.setMethod(className + "." + methodName + "()");

		// 设置操作类型
		if (appLog.getLogType() == 2) {
			appLog.setOperateType(getOperateType(methodName, syslog.operateType()));
		}
		// 请求的参数
		Object[] args = joinPoint.getArgs();
		try {
			String params = JSONObject.toJSONString(args);
			appLog.setRequestParam(params);
		} catch (Exception e) {

		}
		// 获取request
		HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
		// 设置IP地址
		appLog.setIp(IPUtils.getIpAddr(request));
		// 耗时
		appLog.setCostTime(time);
		appLog.setCreateTime(new Date());
		// 保存系统日志
		appLogService.save(appLog);
	}

	/**
	 * 获取操作类型
	 */
	private int getOperateType(String methodName, int operateType) {
		if (operateType > 0) {
			return operateType;
		}
		if (methodName.startsWith("list")) {
			return 1;
		}
		if (methodName.startsWith("add")) {
			return 2;
		}
		if (methodName.startsWith("edit")) {
			return 3;
		}
		if (methodName.startsWith("delete")) {
			return 4;
		}
		if (methodName.startsWith("import")) {
			return 5;
		}
		if (methodName.startsWith("export")) {
			return 6;
		}
		return 1;
	}
}
